package com.okami.entity;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author: TanJingyu
 * @create: 2020-07-03 14:00
 **/
@Data
@Accessors(chain = true)
public class User implements UserDetails {
    private String userName;
    private String password;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 角色，使用@PreAuthorize("hasRole('admin')")
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("ROLE_admin");
        // 操作权限，使用@PreAuthorize("hasAuthority('queryAll')")
        SimpleGrantedAuthority normal = new SimpleGrantedAuthority("queryAll");

        //使用官方工具类构造
//        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList("ROLE_admin", "queryAll");

        return Arrays.asList(admin,normal);
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public boolean isActive() {
        return true;
    }
}
